package cz.data.common.database.metadata.parser;

import cz.data.common.exception.DataException;
import cz.data.common.support.metadata.SqlserverDataTypeEnum;
import cz.data.common.support.ColumnParser;
import cz.data.common.support.DataType;
import cz.data.common.support.DataTypeEnum;

import java.util.Optional;

public class SqlserverColumnParser implements ColumnParser {

    @Override
    public DataType parse(DataTypeEnum dataTypeEnum) {
        SqlserverDataTypeEnum typeEnum = SqlserverDataTypeEnum.match(dataTypeEnum.getValue(), SqlserverDataTypeEnum.VARCHAR);
        switch (typeEnum) {
            case CHAR:
            case NCHAR:
                return new CharDataType();
            case VARCHAR:
            case NVARCHAR:
                return new VarcharDataType();
            case TEXT:
                return new TextDataType();
            case TINYINT:
            case SMALLINT:
            case INT:
                return new IntDataType();
            case FLOAT:
                return new FloatDataType();
            case DECIMAL:
                return new DecimalDataType();
            case DATETIME:
                return new DateTimeDataType();
            case TIMESTAMP:
                return new TimestampDataType();
            case BINARY:
                return new BinaryDataType();
            case VARBINARY:
                return new VarBinaryDataType();
            case IMAGE:
                return new ImageDataType();
            default:
                throw new DataException("未知类型错误");
        }
    }

    public static class CharDataType implements DataType {
        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "CHAR(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }

    public static class VarcharDataType implements DataType {
        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "NVARCHAR(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }

    public static class TextDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TEXT";
        }
    }

    public static class IntDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "INT";
        }
    }

    public static class DecimalDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "DECIMAL";
        }
    }

    public static class FloatDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "FLOAT";
        }
    }

    public static class DateTimeDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "DATETIME";
        }
    }

    public static class TimestampDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TIMESTAMP";
        }
    }

    public static class BinaryDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "BINARY(" + Optional.ofNullable(columnLength).orElse("8000") + ")";
        }
    }

    public static class VarBinaryDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "VARBINARY(" + Optional.ofNullable(columnLength).orElse("8000") + ")";
        }
    }

    public static class ImageDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "IMAGE";
        }
    }
}
